﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using VswService_V7.Utility;

namespace VswService_V7.DataBase.Tables
{
    public class PRODUCTION_STANDARD
    {
        #region field  
        public static readonly string tableName = "PRODUCTION_STANDARD";
        public static readonly string fd_standardNo = "STANDARD_NO";
        public static readonly string fd_standardName = "STANDARD_NAME";
        public static readonly string fd_updateTime = "UPDATE_TIME";
        #endregion
        /// <summary>
        /// get standardno by standardname
        /// </summary>
        /// <param name="standardName"></param>
        /// <returns></returns>
        public static string GetStandardNoByProStandard(string standardName)
        {
            try
            {
                string sql = string.Format("select {0} from {1} where {2}='{3}'", fd_standardNo, tableName, fd_standardName, standardName);
                DataTable dataTable = OracleHelper.Exct_QueryTable(sql);
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    return dataTable.Rows[0][fd_standardNo].ToString();
                }
                else
                {
                    return "0";
                }
            }
            catch
            {
                return "0";
            }
        }
        /// <summary>
        /// get all standard information
        /// </summary>
        /// <returns></returns>
        public static DataTable SelectProductionStandard()
        {
            try
            {
                string sql = string.Format("select ROWNUM rn,a.* from (select * from {0} order by {1} desc) a", tableName, fd_updateTime);
                DataTable dataTable = OracleHelper.Exct_QueryTable(sql);
                return dataTable;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("SelectProductionStandard", ex);
                throw ex;
            }
        }
        /// <summary>
        /// insert PRODUCTION_STANDARD
        /// </summary>
        /// <param name="standardCode"></param>
        /// <param name="standardName"></param>
        /// <returns></returns>
        public static DataTable Insert_ProductionStandard(string standardCode, string standardName)
        {
            try
            {
                string sql = string.Format("insert into {0} values('{1}','{2}','{3}')", tableName,
                  standardCode, standardName, DateTime.Now.ToString(Formats.GetDateFormat())
                  );
                if (OracleHelper.Exct_NonQuery(sql) > 0)
                {
                    LogHelper.WriteLog("ExcuteNewBatchSampleRule Success");
                    return SelectProductionStandard();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("Insert_ProductionStandard", ex);
                throw ex;
            }
        }
        /// <summary>
        /// update PRODUCTION_STANDARD information
        /// </summary>
        /// <param name="standardName"></param>
        /// <returns></returns>
        public static DataTable Edit_ProductionStandard(string standardCode, string standardName)
        {
            try
            {
                string sql = string.Format("update {0} set {1}='{2}' where {3}='{4}'", tableName, fd_standardName, standardName, fd_standardNo, standardCode);
                if (OracleHelper.Exct_NonQuery(sql) > 0)
                {
                    return SelectProductionStandard();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("Edit_ProductionStandard", ex);
                throw ex;
            }
        }
    }
}
